import 'package:flutter/material.dart';
import 'messages.g.dart'; // 导入 NativeBridge 类

class NativeBridgePage extends StatefulWidget {
  const NativeBridgePage({super.key, required this.title});
  final String title;

  @override
  State<NativeBridgePage> createState() => _NativeBridgePageState();
}

class _NativeBridgePageState extends State<NativeBridgePage> {
  int? _batteryLevel;
  String? _eventData;
  String? _messages;

  void _getBatteryLevel() async {
    final batteryLevel =
        await NativeBridge().getBatteryLevel(); // 调用 NativeBridge 上的方法
    setState(() {
      _batteryLevel = batteryLevel;
    });
    print('电池电量: $batteryLevel%');
  }

  @override
  void initState() {
    super.initState();
    NativeBridge().init(); // 初始化 NativeBridge
  }

  _testStreamCall() async {
    final eventData =
        await NativeBridge().testStreamCall(); // 调用 NativeBridge 上的方法
    setState(() {
      _eventData = eventData;
    });
  }

  _testBasicChannel() async {
    final result =
        await NativeBridge().testBasicChannel(); // 调用 NativeBridge 上的方法
    setState(() {
      _messages = result;
    });
    print('result: $result');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: _getBatteryLevel, // 点击按钮时获取电池电量
              child: const Text("获取电池电量"),
            ),
            ElevatedButton(
              onPressed: () => _testStreamCall(),
              child: const Text("_testStreamCall"),
            ),
            ElevatedButton(
              onPressed: () => _testBasicChannel(),
              child: const Text("_testBasicChannel"),
            ),
            if (_batteryLevel != null) Text("电池电量: $_batteryLevel%"),
            if (_eventData != null) Text("_eventData: $_eventData"),
            if (_messages != null) Text("_messages: $_messages"),
          ],
        ),
      ),
    );
  }
}
